package com.example.userservice.mapper;

import com.example.userservice.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Optional;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM users WHERE username = #{username}")
    Optional<User> findByUsername(String username);

    @Select("SELECT * FROM users WHERE user_id = #{id}")
    Optional<User> findById(Long id);

    @Insert("INSERT INTO users(user_id, username, password, email, phone, gmt_create) " +
            "VALUES(#{userId}, #{username}, #{password}, #{email}, #{phone}, #{gmtCreate})")
    void save(User user);

    @Update("UPDATE users SET username=#{username}, password=#{password}, email=#{email}, phone=#{phone} WHERE user_id = #{userId}")
    void update(User user);

    @Select("SELECT * FROM users")
    List<User> findAll();
} 